// pages/manageClinicInfo/manageClinicInfo.js
Page({
  data: {
    clinicInfo: {
      clinicName: '',
      clinicTime: '',
      clinicPhone: '',
      clinicAddress: '',
      clinicLongitude: '',
      clinicLatitude: '',
      clinicDescription: '',
    },
  },
  //从数据库获取诊所信息
  onLoad() {
    wx.cloud.callFunction({
      name: "clinicInfo",
      data: {
        action: "find",
      }
    }).then(res => {
      console.log('获取数据库中的诊所信息成功', res)
      this.setData({
        clinicInfo: res.result.data[0]
      })
      console.log('获取数据库信息后本地的诊所信息', this.data.clinicInfo)
    }).catch(res => {
      console.log('获取数据库中的诊所信息失败', res)
    })
  },

  //获得诊所名称
  getClinicName(e) {
    var clinicName = "clinicInfo.clinicName"
    console.log(e.detail.value)
    
      this.setData({
        [clinicName]: e.detail.value
      })
    
  },
  //获得诊所营业时间
  getClinicTime(e) {
    var clinicTime = "clinicInfo.clinicTime"
    console.log(e.detail.value)
  
      this.setData({
        [clinicTime]: e.detail.value
      })
    
  },
  //获得诊所联系电话
  getClinicPhone(e) {
    var clinicPhone = "clinicInfo.clinicPhone"
    console.log(e.detail.value)
    
      this.setData({
        [clinicPhone]: e.detail.value
      })
    
  },
  //获得诊所地址
  getClinicAddress(e) {
    var clinicAddress = "clinicInfo.clinicAddress"
    console.log(e.detail.value)
    
      this.setData({
        [clinicAddress]: e.detail.value
      })
    
  },
  //获得诊所经度
  getClinicLongitude(e) {
    var clinicLongitude = "clinicInfo.clinicLongitude"
    console.log(e.detail.value)
    
      this.setData({
        [clinicLongitude]: e.detail.value
      })
    
  },
  //获得诊所纬度
  getClinicLatitude(e) {
    var clinicLatitude = "clinicInfo.clinicLatitude"
    console.log(e.detail.value)
    
      this.setData({
        [clinicLatitude]: e.detail.value
      })
    
  },
  //获得诊所简介
  getClinicDescription(e) {
    var clinicDescription = "clinicInfo.clinicDescription"
    console.log(e.detail.value)
   
      this.setData({
        [clinicDescription]: e.detail.value.replace(/\s+/g, '')
      })
    
  },

  //提交
  commit() {
    //验证必填
    if(!this.data.clinicInfo.clinicName){
      wx.showToast({
        icon:'none',
        title: '诊所名不能为空',
      })
      return
    }
    if(!this.data.clinicInfo.clinicTime){
      wx.showToast({
        icon:'none',
        title: '营业时间不能为空',
      })
      return
    }
    if(!this.data.clinicInfo.clinicPhone){
      wx.showToast({
        icon:'none',
        title: '所联系电话不能为空',
      })
      return
    }
    if(!this.data.clinicInfo.clinicAddress){
      wx.showToast({
        icon:'none',
        title: '诊所地址不能为空',
      })
      return
    }
    if(!this.data.clinicInfo.clinicLongitude){
      wx.showToast({
        icon:'none',
        title: '诊所经度不能为空',
      })
      return
    }
    if(!this.data.clinicInfo.clinicLatitude){
      wx.showToast({
        icon:'none',
        title: '诊所纬度不能为空',
      })
      return
    }
    if(!this.data.clinicInfo.clinicDescription){
      wx.showToast({
        icon:'none',
        title: '诊所简介不能为空',
      })
      return
    }
    wx.showLoading({
      title: '信息修改中...',
    })
    wx.cloud.callFunction({
      name: "clinicInfo",
      data: {
        action: "change",
        clinicName: this.data.clinicInfo.clinicName,
        clinicTime: this.data.clinicInfo.clinicTime,
        clinicPhone: this.data.clinicInfo.clinicPhone,
        clinicAddress: this.data.clinicInfo.clinicAddress,
        clinicLongitude: this.data.clinicInfo.clinicLongitude,
        clinicLatitude: this.data.clinicInfo.clinicLatitude,
        clinicDescription: this.data.clinicInfo.clinicDescription,
      }
    }).then(res => {
      console.log('提交成功', res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            title: '修改信息成功',
          })
          setTimeout(function(){
            wx.switchTab({
              url: '/pages/clinic/clinic',
            })
          },1000)
          
        },
      })
     
    }).catch(res => {
      console.log('提交失败', res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            icon:'none',
            title: '修改信息失败',
          })
        },
      })
    })
  },











})